<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handledellog"> 清空日志 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <RoleDrawer @register="registerDrawer" @success="handleDelete" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { getoplogbypage, exlog, oplog } from '../../system-api';

  import { useDrawer } from '/@/components/Drawer';
  import RoleDrawer from './oplogDrawer.vue';

  import { columns, searchFormSchema } from './oplog.data';
  import { useMessage } from '/@/hooks/web/useMessage';
  export default defineComponent({
    name: 'oplogManagement',
    components: { BasicTable, RoleDrawer, TableAction },
    setup() {
      const { createMessage, createConfirm } = useMessage();
      const [registerDrawer, { openDrawer }] = useDrawer();
      const [registerTable, { reload }] = useTable({
        title: '操作日志',
        api: getoplogbypage,
        fetchSetting: {
          pageField: 'page',
          sizeField: 'pageSize',
          listField: 'rows',
          totalField: 'totalRows',
        },
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        actionColumn: {
          width: 80,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: undefined,
        },
        handleSearchInfoFn(info) {
          console.log(info);
          return info;
        },
      });
      //清空日志
      async function handledellog() {
        const log = createConfirm({
          iconType: 'warning',
          title: '提示',
          content: '是否清空所有日志',
          async onOk() {
            console.log(log);
            await oplog.sysOpLogDeletePost();
            reload();
          },
        });
      }

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
        });
      }

      async function handleDelete(record: Recordable) {
        reload();
      }

      function handleSuccess() {
        reload();
      }

      return {
        registerTable,
        registerDrawer,
        handledellog,
        handleEdit,
        handleDelete,
        handleSuccess,
      };
    },
  });
</script>
